package com.handle;

import com.constant.ChannelMap;
import com.message.register.RpcRegisterRequest;
import com.message.register.RpcRegisterResponse;
import com.register.RegisterInfo;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * @author linyu.dai
 * @project rpc
 * @description 注册请求处理器
 * @creat 2022/11/27 16:06:14
 */
@Slf4j
public class RegisterRequestHandle extends SimpleChannelInboundHandler<RpcRegisterRequest> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, RpcRegisterRequest msg) throws Exception {
        RpcRegisterResponse rpcRegisterResponse = new RpcRegisterResponse();
        //注册信息
        RegisterInfo.register(msg,rpcRegisterResponse);
        //保存每个channel和服务名
        ChannelMap.setChannelServer(ctx.channel(), msg.getApplicationName());
        log.info("当前注册中心服务个数: {}",ChannelMap.CHANNEL_SERVER.size());
        //通知现在存在的服务 有服务上线
        ChannelMap.notifyClient(msg.getApplicationName(), msg.getHost(), ctx.channel());
        ctx.channel().writeAndFlush(rpcRegisterResponse);
    }
}
